
#%%
from pprint import pprint
import json

from . import factory

def chart_list(chart_name):
    type_list = ["area","bar","column","gauge","line","liquid","pie","scatter"]
    return_list = []
    for chart in type_list:
        return_list.append(
            {
                "lable":chart,
                "value":chart.replace('-','_')
            }
        )
    return {
            "type": 'select',
            "name": "图表类型",
            "content1":return_list,
            "placeholder": '请选择要查看的图表',
            "id": "chart",
            "value":chart_name
    }
    
def form_html(key,key_value,key_type,data):
    if key == 'data':
        return ''
    # print('form_html',key_type,type(key_type))
    if key_type == str:
        if key in ['xField','yField','seriesField']:
            return special_keyword(key,key_value,data)
        # print(key)
        return {
            "name":key,
            "type":"str",
            "value":key_value,
            "id":key
        }
        
    if key_type ==  bool:
        return {
          "name":key,
          "type": "bool",
          "value":key_value,
          "id":key
        }
    
    if key_type == int or key_type == float :
        if key_type == float:
            key_value *= 100
        return {
          "name":key,
          "type": "int",
          "value":key_value,
          "id":key
        }

def special_keyword(key,key_value,data):
    
    if key in ['xField','yField','seriesField']:
        content1 = []
        for data_key in data[0].keys():
            select_items = {
                "lable": data_key,
                "value": data_key}
            content1.append(select_items)
        if not key_value in data[0].keys():
            key_value = list(data[0].keys())[0]
            # print(key_value)
        return {
            "type": 'select',
            "name": key,
            "content1":content1,
            "placeholder": '请选择内容',
            "id": key,
            "value":key_value
        }

def get_form_tags(set_options,data):
    html_ = []
    for key in set_options.keys():
        if type(set_options[key]) in [dict,list]:
            continue
        list_return = form_html(key,
                                set_options[key],
                                type(set_options[key]),
                                data)
        if list_return:
            html_.append(list_return)
    return html_

def get_ui_data(chart):
    plots_return = getattr(factory[chart], 'example_params')
    set_options = plots_return()['set_options']
    try:
        data = plots_return()['data']
    except:
        data = [plots_return()['data1'],plots_return()['data2']]
    # print(set_options)
    chart_ = chart_list(chart)
    form_ = get_form_tags(set_options,data)
    # pprint([chart_]+form_)
    return [chart_]+form_

def get_ui_my_data(chart, data):
    plots_return = getattr(factory[chart], 'example_params')
    set_options = plots_return()['set_options']
    # print(set_options)
    chart_ = chart_list(chart)
    form_ = get_form_tags(set_options,data)
    # pprint([chart_]+form_)
    return [chart_]+form_

# print(get_ui_data("line"))



# %%
